package com.lp.chapter001.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lp.chapter001.dto.StudentDTO;
import com.lp.chapter001.entity.Student;
import com.lp.chapter001.mapper.StudentMapper;
import com.lp.chapter001.service.StudentService;
import com.lp.chapter001.util.ResultData;
import com.lp.chapter001.vo.PageVO;
import com.lp.chapter001.vo.StudentVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;

/**
 * @author admin
 * @description 针对表【student】的数据库操作Service实现
 * @createDate 2024-02-23 17:27:49
 */
@Service
@AllArgsConstructor
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
        implements StudentService {

    private final StudentMapper studentMapper;

    /**
     * 学生列表
     * @author LP to 2024/3/3
     */
    @Override
    public ResultData<PageVO<StudentVO>> studentPageList(StudentDTO dto) {
        Page<Student> page = new Page<>(dto.getPageNo(), dto.getPageSize());
        QueryWrapper<Student> qw = new QueryWrapper<>();
        qw.select("name", "student_num", "age", "sex", "birthday");
        qw.orderByAsc("student_num");
        Page<Student> studentPage = studentMapper.selectPage(page, qw);
        PageVO<StudentVO> resultVo = new PageVO<>();
        resultVo.setTotalNum(studentPage.getTotal());
        resultVo.setList(studentPage.getRecords().stream().map(o -> {
            StudentVO vo = new StudentVO();
            vo.setName(o.getName());
            vo.setStudentNum(o.getStudentNum());
            vo.setAge(o.getAge());
            vo.setSex(o.getSex());
            vo.setBirthday(DateUtil.format(o.getBirthday(), "yyyy-MM-dd"));
            return vo;
        }).collect(Collectors.toList()));
        return ResultData.success(resultVo);
    }
}




